package com.gandong8.tablestore.web.controller;

import com.gandong8.tablestore.web.domain.common.Result;
import com.gandong8.tablestore.tool.service.inter.TablestoreService;
import com.gandong8.tablestore.web.domain.tablestore.TestTsDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping(value = "/ts",produces = "application/json;charset=UTF-8")
public class TsController {

    @Autowired
    TablestoreService tablestoreService;

    @RequestMapping(value = "/table/create")
    @ResponseBody
    public Result tableCreate() {
        Boolean result  = tablestoreService.createTable(TestTsDO.class);
        return Result.success(result);
    }

    @RequestMapping(value = "/table/delete")
    @ResponseBody
    public Result tableDelete() {
        Boolean result  = tablestoreService.deleteTable(TestTsDO.class);
        return Result.success(result);
    }

    @RequestMapping(value = "/data/get")
    @ResponseBody
    public Result dataGet(String id) {
        TestTsDO obj = new TestTsDO();
        obj.setId(id);
        obj = tablestoreService.getData(obj);
        return Result.success(obj);
    }

    @RequestMapping(value = "/data/put")
    @ResponseBody
    public Result dataPut(String id) {
        TestTsDO obj = new TestTsDO();
        obj.setId(id);
        obj.setCreatedAt(new Date());
        Boolean result = tablestoreService.putData(obj);
        return Result.success(result);
    }

    @RequestMapping(value = "/data/update")
    @ResponseBody
    public Result dataUpdate(String id) {
        TestTsDO obj = new TestTsDO();
        obj.setId(id);
        obj.setCreatedAt(new Date());
        Boolean result = tablestoreService.putData(obj);
        return Result.success(result);
    }

    @RequestMapping(value = "/data/delete")
    @ResponseBody
    public Result dataDelete(String id) {
        TestTsDO obj = new TestTsDO();
        obj.setId(id);
        Boolean result = tablestoreService.deleteData(obj);
        return Result.success(result);
    }

    @RequestMapping(value = "/data/gets")
    @ResponseBody
    public Result dataGets(String id) {
        String[] ids = id.split(",");
        List<TestTsDO> objList = new ArrayList();
        for(String str:ids) {
            TestTsDO obj = new TestTsDO();
            obj.setId(str);
            objList.add(obj);
        }
        objList = tablestoreService.batchGetData(objList);
        return Result.success(objList);
    }

    @RequestMapping(value = "/data/puts")
    @ResponseBody
    public Result dataPuts(String id) {
        String[] ids = id.split(",");
        List<TestTsDO> objList = new ArrayList();
        for(String str:ids) {
            TestTsDO obj = new TestTsDO();
            obj.setId(str);
            obj.setCreatedAt(new Date());
            objList.add(obj);
        }
        tablestoreService.batchPutData(objList);
        return Result.success(true);
    }
}
